<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\FreightWayDemo;
use App\Models\FreightWayDemoData;
use App\Models\Mainborad;

class FreightWayDemoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, FreightWayDemo $freightWayDemo)
    {
        $freightWayDemos = $freightWayDemo->filter($request->all())->paginate(config('api.page.limit'));
        $query['name'] = $request->name;
        return view('admins.freightwaydemo.index', compact('freightWayDemos', 'query'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admins.freightwaydemo.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, FreightWayDemo $freightWayDemo)
    {
        $result = $freightWayDemo->create($request->all());
        if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.freightWayDemos.index')->with('success', '添加成功');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(FreightWayDemo $freightWayDemo)
    {
        return view('admins.freightwaydemo.edit', compact('freightWayDemo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FreightWayDemo $freightWayDemo)
    {
        $result = $freightWayDemo->update($request->all());
        if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.freightWayDemos.index')->with('success', '编辑成功');
    }

    public function show(FreightWayDemo $freightWayDemo)
    {
        dd($freightWayDemo);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request, FreightWayDemo $freightWayDemo)
    {
        $freightWayDemo = $freightWayDemo->find($request->id);
        $freightWayDemo->delete();
        $freightWayDemo->freightWayDemoData()->delete();        
        return json_encode(['code' => 200, 'message' => '删除成功']);
    }


    public function config($id, Request $request, FreightWayDemoData $freightWayDemoData)
    {
        $freightWayDemo = FreightWayDemo::find($id);
        $freightWayDemoData = $freightWayDemoData->where('freight_way_demo_id', $id)->get();
        $freight_num = $freightWayDemo->freight_num;
        return view('admins.freightwaydemo.config', compact('freightWayDemoData','id', 'freight_num'));
    }

    public function configEdit(FreightWayDemoData $freightWayDemoData)
    {
        return view('admins.freightwaydemo.config_edit', compact('freightWayDemoData' ));
    }
    //批量修改货道模板信息
    public function configUpdate(Request $request, FreightWayDemoData $freightWayDemoData)
    {   
        $data = $request->all();
        $fail = [];
        $success = 0;
        $update_data = [];
        if($data['edit_capacity']){
            $update_data['capacity'] = $data['capacity'];
        }
        if($data['edit_type']){
            $update_data['type'] = $data['type'];
        }
        if($data['edit_state']){
            $update_data['state'] = $data['state'];
        }
        if(count($update_data) > 0){
            if(!empty($data['start']) && !empty($data['end'])){
                for ($i=$data['start']; $i <= $data['end']; $i++) { 
                    $res = $freightWayDemoData->where(['freight_way_demo_id' => $data['freight_way_demo_id'], 'No' => $i])->update($update_data);
                    if(!$res){
                        $fail[] = $i;
                    }else{
                        $success++;
                    }
                }
            }elseif(!empty($data['singles'])){
                $singles = explode(',', $data['singles']);
                foreach ($singles as $key => $value) {
                    $res = $freightWayDemoData->where(['freight_way_demo_id' => $data['freight_way_demo_id'], 'No' => $value])->update($update_data);
                    if(!$res){
                        $fail[] = $i;
                    }else{
                        $success++;
                    }
                }
            }
        }        
        return json_encode(['code' => 200, 'msg' =>['success' => $success, 'fail' => count($fail), 'failNo' => implode(',',$fail)] ]);
    }

    //根据主板获取货道模板
    public function lists(Request $request, FreightWayDemo $freightWayDemo)
    {
        return response()->json(['code' => 200, 'msg' => 'success', 'data' => $freightWayDemo->with('freightWayDemoData')->orderBy('id','desc')->get()]);
    }
}
